#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
vector<int> leastDistance(vector<int> array1,vector<int> array2) {
    sort(array1.begin(),array1.end());
    sort(array2.begin(),array2.end());
    int current = INT_MAX;
    int smallest = INT_MAX;
    int idx1 = 0;
    int idx2 = 0;
    vector<int> smallestPair;
    while (idx1 < array1.size() && idx2 < array2.size()) {
        int firstNum = array1[idx1];
        int secondNum = array2[idx2];
        if (firstNum < secondNum) {
            current = secondNum - firstNum;
            idx1 += 1; 
        } 
        else if (firstNum > secondNum) {
            current = firstNum - secondNum;
            idx2 += 1;
        }
        else {
            return {firstNum, secondNum};
        }
        if (current < smallest) {
            smallest = current;
            smallestPair = {firstNum, secondNum};
        }
    }
    return smallestPair;
}
int main(int argc, char const *argv[])
{
    vector<int> array1 {24,65,48,62,72,61,63};
    vector<int> array2 {12,24,76,72,87,86,63};
    vector<int> result = leastDistance(array1, array2);
    for (int n : result) {
        cout<<n<<" ";
    }
    cout<<endl;
    return 0;
}
